package com.sun.impls.users;
import cn.dev33.satoken.util.SaResult;
import com.sun.dto.BaseSearchDto;
import com.sun.dto.users.RoleDto;
import com.sun.entity.users.Role;
import com.sun.entity.users.User;
import com.sun.impls.BaseService;
import com.sun.repository.users.RoleRepository;
import com.sun.services.CustomPredicate;
import com.sun.services.users.RoleService;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoleServiceImpl extends BaseService implements RoleService {
    @Autowired
    private RoleRepository roleRepository;
    @Override
    public SaResult createRole(RoleDto roleDto) {
        Role role = new Role();
        role.setId(nextId());
        role.setName(roleDto.getName());
        role.setDescription(roleDto.getDescription());
        roleRepository.save(role);
        return SaResult.ok("角色创建成功");
    }
    @Override
    public SaResult updateRole(RoleDto roleDto) {
        Role role = roleRepository.findById(roleDto.getId()).orElse(null);
        if (role == null) {
            return SaResult.error("角色不存在");
        }
        role.setName(roleDto.getName());
        role.setDescription(roleDto.getDescription());
        roleRepository.save(role);
        return SaResult.ok("角色更新成功");
    }
    @Override
    public SaResult deleteRole(List<Long> ids) {
        roleRepository.deleteAllById(ids);
        return SaResult.ok("角色删除成功");
    }
    @Override
    public List<Role> getRoleList(BaseSearchDto search) {
        CustomPredicate<Role> predicate = (cb, root) -> basePredicates(cb, root, search);
        return getList(Role.class, predicate, search);
    }
}
